/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.accsoftware.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.sql.Date;
import java.util.List;

import javax.swing.JTable;

import com.accsoftware.common.Table1;
import com.accsoftware.common.Table2;
import com.accsoftware.util.DataHandler;

/**
 *
 * @author YAN
 */
public class SearchAndDisplayPanel extends javax.swing.JPanel {

    /**
	 * 
	 */
	private static final long serialVersionUID = -9078860779973523031L;
	/**
     * Creates new form SerchAndDisplayPanel
     */
    public SearchAndDisplayPanel() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        chooseTableButtonGroup = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        date = new com.toedter.calendar.JDateChooser();
        jLabel2 = new javax.swing.JLabel();
        date1 = new com.toedter.calendar.JDateChooser();
        displayPanel = new javax.swing.JPanel();
        generateReport = new javax.swing.JButton();
        Search = new javax.swing.JButton();
        chooseTablePanel = new javax.swing.JPanel();
        table1RadioButton = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();

        jLabel1.setText("Start:");

        jLabel2.setText("End:");

        javax.swing.GroupLayout displayPanelLayout = new javax.swing.GroupLayout(displayPanel);
        displayPanel.setLayout(displayPanelLayout);
        displayPanelLayout.setHorizontalGroup(
            displayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        displayPanelLayout.setVerticalGroup(
            displayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 173, Short.MAX_VALUE)
        );

        generateReport.setText("Generate Report");
        generateReport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateReportActionPerformed(evt);
            }
        });

        Search.setText("Search");
        Search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SearchActionPerformed(evt);
            }
        });

        chooseTableButtonGroup.add(table1RadioButton);
        table1RadioButton.setText("Table1");

        chooseTableButtonGroup.add(jRadioButton2);
        jRadioButton2.setText("Table2");

        javax.swing.GroupLayout chooseTablePanelLayout = new javax.swing.GroupLayout(chooseTablePanel);
        chooseTablePanel.setLayout(chooseTablePanelLayout);
        chooseTablePanelLayout.setHorizontalGroup(
            chooseTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(chooseTablePanelLayout.createSequentialGroup()
                .addComponent(table1RadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addComponent(jRadioButton2)
                .addContainerGap())
        );
        chooseTablePanelLayout.setVerticalGroup(
            chooseTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(chooseTablePanelLayout.createSequentialGroup()
                .addGroup(chooseTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(table1RadioButton)
                    .addComponent(jRadioButton2))
                .addGap(0, 6, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(displayPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(date, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 79, Short.MAX_VALUE)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(date1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(chooseTablePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(generateReport)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Search)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(date1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(date, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(displayPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(generateReport)
                        .addComponent(Search, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(chooseTablePanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>

    private void generateReportActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    private void SearchActionPerformed(java.awt.event.ActionEvent evt) {
        Date start = dateToString(date);
    	Date end = dateToString(date1);
    	String table = table1RadioButton.isSelected()?"Table1":"Table2";
		List result = DataHandler.readAll(table, start, end);
		int col = table1RadioButton.isSelected()?7:10;
		
		Object[][] data = new Object[result.size()][col];
		
		if(table.equalsIgnoreCase("Table1")){
			int id = 0;
			for(Object element:result){
				Table1 table1 = (Table1) element;
				data[id][0] = table1.getDate();
				data[id][1] = table1.getItem();
				data[id][2] = table1.getFee();
				data[id][3] = table1.getGst();
				data[id][4] = table1.getVehicle();
				data[id][5] = table1.getWages();
				data[id][6] = table1.getStationary();
				id++;			

			}
			String[] columnNames = { "Date", "Reference/Item", "Cost", "GST",
					"Vehicle", "Wages", "Stationary" };
	
			JTable displayTable = new JTable(data, columnNames);
			jScrollPane1 = new javax.swing.JScrollPane();
			jScrollPane1.setViewportView(displayTable);
			/*for(int i = 0; i < data.length; i ++){
				for(int j = 0; j < data[i].length;j++){
					System.out.print(data[i][j] + " ");
				}
				System.out.println();
			}*/
			
		}else{
			int id = 0;
			for(Object element:result){
				Table2 table2 = (Table2) element;
				data[id][0] = table2.getDate();
				data[id][1] = table2.getItem();
				data[id][2] = table2.getFee();
				data[id][3] = table2.getGst();
				data[id][4] = table2.getAccomodation();
				data[id][5] = table2.getTransport();
				data[id][6] = table2.getAdmissions();
				data[id][7] = table2.getParking();
				data[id][8] = table2.getDining();
				data[id][9] = table2.getWages();
				id++;			

			}
			String[] columnNames = { "Date", "Reference/Item", "Cost", "GST",
					"Accomodation", "Transport", "Admissions", "Parking", "Dining", "Wages" };
	
			JTable displayTable = new JTable(data, columnNames);
			jScrollPane1 = new javax.swing.JScrollPane();
			jScrollPane1.setViewportView(displayTable);
			
		}
		
		
		this.displayPanel.removeAll();
		jScrollPane1.setBounds(this.displayPanel.getVisibleRect());
		this.displayPanel.setBackground(Color.black);
		jScrollPane1.revalidate();
		jScrollPane1.repaint();
		this.displayPanel.add(jScrollPane1, BorderLayout.NORTH);
		this.displayPanel.revalidate();
		this.displayPanel.repaint();
    }
    
    @SuppressWarnings("deprecation")
	private Date dateToString(com.toedter.calendar.JDateChooser date){
    	return new Date(date.getDate().getYear(),
    			date.getDate().getMonth(), date.getDate().getDay());
    } 
    // Variables declaration - do not modify
    private javax.swing.JButton Search;
    private javax.swing.ButtonGroup chooseTableButtonGroup;
    private javax.swing.JPanel chooseTablePanel;
    private com.toedter.calendar.JDateChooser date;
    private com.toedter.calendar.JDateChooser date1;
    private javax.swing.JPanel displayPanel;
    private javax.swing.JButton generateReport;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JRadioButton table1RadioButton;
    private javax.swing.JRadioButton jRadioButton2;
    javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration
}
